package cn.itcast.controller;

import cn.itcast.domain.Courier;
import cn.itcast.service.CourierService;
import cn.itcast.service.StandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CourierController {
    @Autowired
    private CourierService courierService;
/*    @Autowired
    private StandService standService;*/

    //新增保存
    @RequestMapping("/courier_save")
    public String add(Courier courier) {
        courierService.add(courier);
        return "redirect:/pages/base/courier.html";
    }

    //查询
    @RequestMapping("/courier_pageQuery")
    @ResponseBody
    public Map pageQuery(Courier courier,int page, int rows) {
        Pageable pageRequest = new PageRequest(page - 1, rows);
        Specification<Courier> specification=new Specification<Courier>() {
            @Override
            public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                ArrayList<Object> list = new ArrayList<>();
                //单表查询
                if (org.apache.commons.lang.StringUtils.isNotBlank(courier.getCourierNum())){
                    Predicate p1=criteriaBuilder.equal(root.get("courierNum").as(String.class),courier.getCourierNum());
                    list.add(p1);
                }
                if (org.apache.commons.lang.StringUtils.isNotBlank(courier.getCompany())){
                    Predicate p2=criteriaBuilder.like(root.get("company").as(String.class),courier.getCompany());
                    list.add(p2);
                }
                if (org.apache.commons.lang.StringUtils.isNotBlank(courier.getType())){
                    Predicate p3 = criteriaBuilder.equal(root.get("type").as(String.class), courier.getType());
                    list.add(p3);
                }
                //多表查询
                Join<Object, Object> join = root.join("standard", JoinType.INNER);
                if (courier.getStandard()!=null&& org.apache.commons.lang.StringUtils.isNotBlank(courier.getStandard().getName())){
                   Predicate p4= criteriaBuilder.like(join.get("name").as(String.class),"%"+courier.getStandard().getName()+"%");
                   list.add(p4);
                }
              return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        };
        Page<Courier> couriers = courierService.pageQuery(specification, pageRequest);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", couriers.getTotalElements());
        result.put("rows", couriers.getContent());
        return result;
    }

    //回显
    @RequestMapping("/courier_restoreBatch")
    public String restore(String ids) {
        courierService.restore(ids);
        return "redirect:/pages/base/courier.html";
    }
    //作废
    @RequestMapping("/courier_delBatch")
    public String update(String ids){
        courierService.update(ids);
        return "redirect:/pages/base/courier.html";
    }

}
